package com.lqs.sofp.dao.impl;

import com.lqs.sofp.dao.TextInfoDao;
import com.lqs.sofp.entity.Text;
import com.lqs.sofp.utils.DbUtils;
import java.sql.*;
import java.util.ArrayList;

public class TextInfoDaolmpl implements TextInfoDao {
    public boolean update(Text text) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try{
            String sql = "INSERT INTO TextInfo(Poster,Text,Title) VALUES(?,?,?);";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);

            stmt.setString(1,text.getPoster());
            stmt.setString(2,text.getText());
            stmt.setString(3,text.getTitle());

            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——成功添加——");
                return true;
            } else {
                System.out.println("——添加失败——");
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return true;
    }
    public boolean delete (Text text){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "delete from TextInfo where Poster=? and Text=? and Title=? ";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, text.getPoster());
            stmt.setString(2,text.getText());
            stmt.setString(3,text.getTitle());
            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——删除成功——");
                return true;
            } else {
                System.out.println("——删除失败——");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return false;
    }

    public Text select (String poster){
        Text text=null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try{
            conn = DbUtils.getConnection();
            String sql = "SELECT *FROM TextInfo where Poster = ? ";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, poster);
            rs = stmt.executeQuery();

            rs.last();
            int count = rs.getRow();
            System.out.println(count);
            rs.beforeFirst();
            if(count!=0){
                while (rs.next()){
                    String posterName=rs.getString("Poster");
                    String text=rs.getString("Text");
                    String title=rs.getString("Title");
                    Text temp=new Text(posterName,text,title);
                    text=temp;
                    System.out.println(text.toString());
                }
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return text;
    }

    public ArrayList<Text> selectAll () {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<Text> textList =new ArrayList<>();
        try {
            String sql = "select *from TextInfo";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                String posterName = rs.getString("Poster");
                String text = rs.getString("Text");
                String title = rs.getString("Title");
                Text text = new Text(poster, text, title);
                textList.add(text);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return textList;
    }

}